using APP.HELPER.Data.Helper;
using APP.HELPER.Utils.Language;
using APP.HELPER.Utils.Security;
using APP.HELPER.Utils.UI;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Columns;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
namespace Extra.Common
{
    public class xfmDatabaseConfig : XtraForm
    {
        private bool _exit;
        private bool _isCancel = true;
        private int _part;
        private readonly string _serverParameters = (Application.StartupPath + @"\Data.config.xml");
        private SimpleButton btnCancel;
        private SimpleButton btnHelp;
        private SimpleButton btnLogin;
        private ComboBoxEdit cbxDatabase;
        private ComboBoxEdit cbxServer;
        private CheckEdit ckDataExample;
        private IContainer components = null;
        private const string DefData = "HRMExample";
        private DevExpress.Utils.ImageCollection imageCollection2;
        private LabelControl lblDatabase;
        private LabelControl lblSelectdatabase;
        private LabelControl lblServer;
        private LabelControl lblSqlpassword;
        private LabelControl lblSqlusername;
        private PictureEdit picRefreshdatabase;
        private PictureEdit picRefreshserver;
        private RadioGroup rdgAuthentication;
        private LoadingCircle spWait;
        private TextEdit teLogin;
        private TextEdit tePassword;
        protected TreeListColumn tlcServer;
        private TreeList trlServer;

        public event CancelEventHander Cancel;

        private event CompleteEventHander Complete;

        private event DatabasedEventHander Databased;

        public event DatabaseErrorEventHander DatabaseError;

        public event LoginedEventHander Logined;

        private event ProcessEventHander Process;

        private event StartedEventHander Started;

        public xfmDatabaseConfig()
        {
            this.InitializeComponent();
            this.InitMultiLanguages();
            this.cbxServer.Text = Environment.MachineName + @"\PERFECT";
            this.Started = (StartedEventHander)Delegate.Combine(this.Started, new StartedEventHander(this.xfmLogin_Started));
            this.Complete = (CompleteEventHander)Delegate.Combine(this.Complete, new CompleteEventHander(this.xfmLogin_Complete));
            this.Databased = (DatabasedEventHander)Delegate.Combine(this.Databased, new DatabasedEventHander(this.xfmLogin_Databased));
            this.DatabaseError = (DatabaseErrorEventHander)Delegate.Combine(this.DatabaseError, new DatabaseErrorEventHander(this.xfmLogin_DatabaseError));
            this.ReadCofig();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this._isCancel = true;
            base.Close();
        }

        private void btnDatabaseOther_Click(object sender, EventArgs e)
        {
            this.cbxDatabase.Properties.Items.Clear();
            if (this.cbxDatabase.Properties.Items.Count == 0)
            {
                new Thread(new ThreadStart(this.DatabaseReload)).Start();
                if (this.cbxDatabase.Properties.Items.Count > 0)
                {
                    this.cbxDatabase.SelectedIndex = -1;
                }
            }
        }

        private void BtnHelpClick(object sender, EventArgs e)
        {
            FileInfo info = new FileInfo(Application.StartupPath + @"\QLCSDL.wmv");
            if (info.Exists)
            {
                System.Diagnostics.Process.Start(info.FullName);
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (this.cbxDatabase.Text != "")
            {
                string dataBaseConnectionString = this.GetDataBaseConnectionString();
                if (this.CorrectConnection(dataBaseConnectionString))
                {
                    if (this._part == 0)
                    {
                        this.SaveConfig(this.cbxServer.Text, this.rdgAuthentication.SelectedIndex, this.teLogin.Text, this.tePassword.Text, this.cbxDatabase.Text, dataBaseConnectionString);
                    }
                    SqlHelper.ConnectString = dataBaseConnectionString;
                    this._isCancel = false;
                    this.RaiseLoginedEventHander(dataBaseConnectionString);
                    base.Close();
                }
            }
        }

        private void btnOrther_Click(object sender, EventArgs e)
        {
            new Thread(new ThreadStart(this.loadserver)).Start();
        }

        private void cbxDatabase_Click(object sender, EventArgs e)
        {
        }

        private void cbxDatabase_EditValueChanged(object sender, EventArgs e)
        {
            this.ckDataExample.Checked = this.cbxDatabase.Text == "HRMExample";
        }

        private void cbxDatabase_Properties_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            if ((((string)e.Button.Tag) == "load") && (this.cbxDatabase.Properties.Items.Count == 0))
            {
                new Thread(new ThreadStart(this.DatabaseReload)).Start();
                if (this.cbxDatabase.Properties.Items.Count > 0)
                {
                    this.cbxDatabase.SelectedIndex = -1;
                }
            }
        }

        private void cbxDatabase_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void cbxServer_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.cbxDatabase.Properties.Items.Clear();
        }

        private void CbxServerKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == System.Windows.Forms.Keys.Enter)
            {
                this.btnLogin_Click(this, null);
            }
        }

        private void ckDataExample_CheckedChanged(object sender, EventArgs e)
        {
            DataTable table = new DataTable("SERVER");
            table.Columns.Add("server");
            table.Columns.Add("auth");
            table.Columns.Add("user");
            table.Columns.Add("pass");
            table.Columns.Add("database");
            table.Columns.Add("ConnectString");
            SqlHelper helper = new SqlHelper();
            FileInfo info = new FileInfo(this._serverParameters);
            if (info.Exists)
            {
                DataSet set = new DataSet();
                set.Tables.Add(table);
                set.ReadXml(this._serverParameters, XmlReadMode.Auto);
                try
                {
                    if (set.Tables[0].Rows.Count > 0)
                    {
                        helper.Server = set.Tables[0].Rows[0]["server"].ToString();
                        helper.Authentication = Convert.ToInt32(set.Tables[0].Rows[0]["auth"]) == 0;
                        helper.UserID = MyEncryption.Decrypt(set.Tables[0].Rows[0]["user"].ToString(), "963147", true);
                        helper.Password = MyEncryption.Decrypt(set.Tables[0].Rows[0]["pass"].ToString(), "963147", true);
                        helper.Database = set.Tables[0].Rows[0]["database"].ToString();
                        helper.ConnectionString = MyEncryption.Decrypt(set.Tables[0].Rows[0]["ConnectString"].ToString(), "963147", true);
                        if (helper.Database == "HRMExample")
                        {
                            if (this.ckDataExample.Checked)
                            {
                                this.cbxDatabase.Text = "HRMExample";
                            }
                            else
                            {
                                this.cbxServer.Text = helper.Server;
                                this.teLogin.Text = helper.UserID;
                                this.tePassword.Text = helper.Password;
                                this.rdgAuthentication.SelectedIndex = helper.Authentication ? 0 : 1;
                                if (this.cbxDatabase.Properties.Items.Count == 0)
                                {
                                    new Thread(new ThreadStart(this.DatabaseReload)).Start();
                                    if (this.cbxDatabase.Properties.Items.Count > 0)
                                    {
                                        this.cbxDatabase.SelectedIndex = -1;
                                    }
                                }
                            }
                        }
                        else if (this.ckDataExample.Checked)
                        {
                            this.cbxDatabase.Text = "HRMExample";
                        }
                        else
                        {
                            this.cbxServer.Text = helper.Server;
                            this.teLogin.Text = helper.UserID;
                            this.tePassword.Text = helper.Password;
                            this.cbxDatabase.Text = helper.Database;
                            this.rdgAuthentication.SelectedIndex = helper.Authentication ? 0 : 1;
                        }
                    }
                }
                catch (Exception exception)
                {
                    XtraMessageBox.Show(exception.Message, "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
            }
        }

        private void CloseForm()
        {
            base.TopMost = false;
            if (this.CorrectConnection(this.GetServerConnectionString()))
            {
                try
                {
                    using (XmlTextWriter writer = new XmlTextWriter(this._serverParameters, Encoding.UTF8))
                    {
                        writer.WriteElementString("Parameters", this.ConnectionStringParameters);
                    }
                }
                catch
                {
                }
            }
        }

        private SqlConnection Connection(string serverConnectionString)
        {
            Cursor current = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;
            SqlConnection connection = new SqlConnection(serverConnectionString);
            try
            {
                connection.Open();
            }
            catch
            {
                this.RaiseDatabaseErrorEventHander("Kh\x00f4ng thể kết nối được m\x00e1y chủ.");
                return connection;
            }
            finally
            {
                Cursor.Current = current;
            }
            return connection;
        }

        private bool CorrectConnection(string serverConnectionString)
        {
            Cursor current = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;
            using (SqlConnection connection = new SqlConnection(serverConnectionString))
            {
                try
                {
                    connection.Open();
                    connection.Close();
                }
                catch
                {
                    XtraMessageBox.Show(this, "Kh\x00f4ng thể kết nối với m\x00e1y chủ.", "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    return false;
                }
                finally
                {
                    Cursor.Current = current;
                }
            }
            return true;
        }

        private void DatabaseReload()
        {
            Cursor.Current = Cursors.WaitCursor;
            this.RaiseStartedEventHander();
            DataTable data = new DataTable();
            data.Columns.Add("Database");
            try
            {
                SqlConnection sqlConnection = this.Connection(this.GetServerConnectionString());
                if (sqlConnection.State != ConnectionState.Open)
                {
                    return;
                }
                ServerConnection serverConnection = new ServerConnection(sqlConnection);
                Server server = new Server(serverConnection);
                foreach (Database database in server.Databases)
                {
                    try
                    {
                        if (this._part == 0)
                        {
                            if (database.ExecuteWithResults("select * from SYS_INFO").Tables[0].Rows.Count > 0)
                            {
                                data.Rows.Add(new object[] { database.Name });
                            }
                        }
                        else
                        {
                            data.Rows.Add(new object[] { database.Name });
                        }
                    }
                    catch
                    {
                    }
                }
            }
            catch (Exception exception)
            {
                this.RaiseDatabaseErrorEventHander(exception.Message);
            }
            this.RaiseDatabasedEventHander(data);
        }

        private void Disable(bool disable)
        {
            this.trlServer.Enabled = disable;
            this.cbxServer.Enabled = disable;
            this.rdgAuthentication.Enabled = disable;
            this.cbxDatabase.Enabled = disable;
            this.btnLogin.Enabled = disable;
            this.btnCancel.Enabled = disable;
            this.btnHelp.Enabled = disable;
            this.picRefreshserver.Enabled = disable;
            this.picRefreshdatabase.Enabled = disable;
            this.ckDataExample.Enabled = disable;
        }

        private void DisableSQLServerAuthentication(bool disable)
        {
            this.teLogin.Enabled = !disable;
            this.tePassword.Enabled = !disable;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        public string GetDataBaseConnectionString()
        {
            string str = string.Format("data source={0};integrated security=SSPI;Initial Catalog={1};", this.cbxServer.Text, this.cbxDatabase.Text);
            if (this.rdgAuthentication.SelectedIndex == 1)
            {
                str = string.Format("server={0};user id={1};password={2};database={3};", new object[] { this.cbxServer.Text, this.teLogin.Text, this.tePassword.Text, this.cbxDatabase.Text });
            }
            return str;
        }

        private string GetServerConnectionString()
        {
            string str = string.Format("data source={0};integrated security=SSPI;Initial Catalog=master;", this.cbxServer.Text);
            if (this.rdgAuthentication.SelectedIndex == 1)
            {
                str = string.Format("server={0};user id={1};password={2};", this.cbxServer.Text, this.teLogin.Text, this.tePassword.Text);
            }
            return str;
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            ComponentResourceManager manager = new ComponentResourceManager(typeof(xfmDatabaseConfig));
            SerializableAppearanceObject appearance = new SerializableAppearanceObject();
            this.lblServer = new LabelControl();
            this.rdgAuthentication = new RadioGroup();
            this.tePassword = new TextEdit();
            this.teLogin = new TextEdit();
            this.lblSqlpassword = new LabelControl();
            this.lblSqlusername = new LabelControl();
            this.btnLogin = new SimpleButton();
            this.btnCancel = new SimpleButton();
            this.btnHelp = new SimpleButton();
            this.cbxServer = new ComboBoxEdit();
            this.cbxDatabase = new ComboBoxEdit();
            this.lblDatabase = new LabelControl();
            this.trlServer = new TreeList();
            this.tlcServer = new TreeListColumn();
            this.imageCollection2 = new DevExpress.Utils.ImageCollection(this.components);
            this.lblSelectdatabase = new LabelControl();
            this.picRefreshdatabase = new PictureEdit();
            this.picRefreshserver = new PictureEdit();
            this.spWait = new LoadingCircle();
            this.ckDataExample = new CheckEdit();
            this.rdgAuthentication.Properties.BeginInit();
            this.tePassword.Properties.BeginInit();
            this.teLogin.Properties.BeginInit();
            this.cbxServer.Properties.BeginInit();
            this.cbxDatabase.Properties.BeginInit();
            this.trlServer.BeginInit();
            this.imageCollection2.BeginInit();
            this.picRefreshdatabase.Properties.BeginInit();
            this.picRefreshserver.Properties.BeginInit();
            this.ckDataExample.Properties.BeginInit();
            base.SuspendLayout();
            this.lblServer.Location = new Point(240, 0x1d);
            this.lblServer.Name = "lblServer";
            this.lblServer.Size = new Size(0x2a, 13);
            this.lblServer.TabIndex = 0;
            this.lblServer.Text = "M\x00e1y Chủ";
            this.lblServer.Click += new EventHandler(this.lblServer_Click);
            this.rdgAuthentication.EditValue = 0;
            this.rdgAuthentication.Location = new Point(0x132, 0x34);
            this.rdgAuthentication.Name = "rdgAuthentication";
            this.rdgAuthentication.Properties.Appearance.BackColor = Color.Transparent;
            this.rdgAuthentication.Properties.Appearance.Options.UseBackColor = true;
            this.rdgAuthentication.Properties.Columns = 1;
            this.rdgAuthentication.Properties.Items.AddRange(new RadioGroupItem[] { new RadioGroupItem(0, "T\x00e0i Khoản Windows"), new RadioGroupItem(1, "T\x00e0i Khoản SQL Server") });
            this.rdgAuthentication.Size = new Size(0xa8, 0x30);
            this.rdgAuthentication.TabIndex = 3;
            this.rdgAuthentication.SelectedIndexChanged += new EventHandler(this.radioGroup1_SelectedIndexChanged);
            this.rdgAuthentication.KeyDown += new KeyEventHandler(this.CbxServerKeyDown);
            this.tePassword.Enabled = false;
            this.tePassword.Location = new Point(0x132, 130);
            this.tePassword.Name = "tePassword";
            this.tePassword.Properties.PasswordChar = '*';
            this.tePassword.Size = new Size(0xa8, 20);
            this.tePassword.TabIndex = 0x17;
            this.tePassword.KeyDown += new KeyEventHandler(this.CbxServerKeyDown);
            this.teLogin.EditValue = "sa";
            this.teLogin.Enabled = false;
            this.teLogin.Location = new Point(0x132, 0x6a);
            this.teLogin.Name = "teLogin";
            this.teLogin.Size = new Size(0xa8, 20);
            this.teLogin.TabIndex = 0x16;
            this.teLogin.KeyDown += new KeyEventHandler(this.CbxServerKeyDown);
            this.lblSqlpassword.Location = new Point(240, 0x85);
            this.lblSqlpassword.Name = "lblSqlpassword";
            this.lblSqlpassword.Size = new Size(0x2d, 13);
            this.lblSqlpassword.TabIndex = 0x19;
            this.lblSqlpassword.Text = "Mật Khẩu";
            this.lblSqlusername.Location = new Point(240, 0x6d);
            this.lblSqlusername.Name = "lblSqlusername";
            this.lblSqlusername.Size = new Size(0x2f, 13);
            this.lblSqlusername.TabIndex = 0x18;
            this.lblSqlusername.Text = "T\x00e0i Khoản";
            this.btnLogin.Image = (Image)manager.GetObject("btnLogin.Image");
            this.btnLogin.Location = new Point(240, 0xd4);
            this.btnLogin.Name = "btnLogin";
            this.btnLogin.Size = new Size(80, 0x19);
            this.btnLogin.TabIndex = 0x1b;
            this.btnLogin.Text = "Thực Hiện";
            this.btnLogin.Click += new EventHandler(this.btnLogin_Click);
            this.btnCancel.Image = (Image)manager.GetObject("btnCancel.Image");
            this.btnCancel.Location = new Point(330, 0xd4);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new Size(80, 0x19);
            this.btnCancel.TabIndex = 0x1c;
            this.btnCancel.Text = "Đ\x00f3ng";
            this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
            this.btnHelp.Image = (Image)manager.GetObject("btnHelp.Image");
            this.btnHelp.Location = new Point(420, 0xd4);
            this.btnHelp.Name = "btnHelp";
            this.btnHelp.Size = new Size(0x55, 0x19);
            this.btnHelp.TabIndex = 0x1d;
            this.btnHelp.Text = "Hướng Dẫn";
            this.btnHelp.Click += new EventHandler(this.BtnHelpClick);
            this.cbxServer.EditValue = @"(Local)\PERFECT";
            this.cbxServer.Location = new Point(0x132, 0x1a);
            this.cbxServer.Name = "cbxServer";
            this.cbxServer.Properties.Buttons.AddRange(new EditorButton[] { new EditorButton(ButtonPredefines.Combo) });
            this.cbxServer.Size = new Size(0xa8, 20);
            this.cbxServer.TabIndex = 0x1f;
            this.cbxServer.SelectedIndexChanged += new EventHandler(this.cbxServer_SelectedIndexChanged);
            this.cbxServer.KeyDown += new KeyEventHandler(this.CbxServerKeyDown);
            this.cbxDatabase.Location = new Point(0x132, 0x9b);
            this.cbxDatabase.Name = "cbxDatabase";
            this.cbxDatabase.Properties.Buttons.AddRange(new EditorButton[] { new EditorButton(ButtonPredefines.Combo, "", -1, true, true, false, ImageLocation.MiddleCenter, null, new KeyShortcut(System.Windows.Forms.Keys.None), appearance, "", "load", null, true) });
            this.cbxDatabase.Properties.ImmediatePopup = true;
            this.cbxDatabase.Properties.NullText = "(Chọn)";
            this.cbxDatabase.Properties.ButtonClick += new ButtonPressedEventHandler(this.cbxDatabase_Properties_ButtonClick);
            this.cbxDatabase.Size = new Size(0xa8, 20);
            this.cbxDatabase.TabIndex = 0x21;
            this.cbxDatabase.EditValueChanged += new EventHandler(this.cbxDatabase_EditValueChanged);
            this.cbxDatabase.SelectedIndexChanged += new EventHandler(this.cbxDatabase_SelectedIndexChanged);
            this.cbxDatabase.KeyDown += new KeyEventHandler(this.CbxServerKeyDown);
            this.cbxDatabase.Click += new EventHandler(this.cbxDatabase_Click);
            this.lblDatabase.Location = new Point(240, 0x9e);
            this.lblDatabase.Name = "lblDatabase";
            this.lblDatabase.Size = new Size(0x39, 13);
            this.lblDatabase.TabIndex = 0x20;
            this.lblDatabase.Text = "T\x00ean Dữ Liệu";
            this.trlServer.Columns.AddRange(new TreeListColumn[] { this.tlcServer });
            this.trlServer.CustomizationFormBounds = new Rectangle(0x22f, 0x15b, 0xd0, 170);
            this.trlServer.Dock = DockStyle.Left;
            this.trlServer.Location = new Point(0, 0);
            this.trlServer.Name = "trlServer";
            this.trlServer.BeginUnboundLoad();
            this.trlServer.AppendNode(new object[] { "M\x00e1y Đơn" }, -1, 0x25, 0x25, -1);
            this.trlServer.AppendNode(new object[] { "Mạng Nội Bộ" }, -1, 0x23, 0x23, -1);
            this.trlServer.AppendNode(new object[] { "Mạng Internet" }, -1, 0x26, 0x26, -1);
            this.trlServer.EndUnboundLoad();
            this.trlServer.SelectImageList = this.imageCollection2;
            this.trlServer.Size = new Size(0xe3, 0xf2);
            this.trlServer.TabIndex = 0x23;
            this.trlServer.FocusedNodeChanged += new FocusedNodeChangedEventHandler(this.trlServer_FocusedNodeChanged);
            this.tlcServer.Caption = "Chọn M\x00e1y Chủ";
            this.tlcServer.FieldName = "tlcServer";
            this.tlcServer.MinWidth = 0x37;
            this.tlcServer.Name = "tlcServer";
            this.tlcServer.OptionsColumn.AllowEdit = false;
            this.tlcServer.OptionsColumn.AllowFocus = false;
            this.tlcServer.OptionsColumn.AllowMove = false;
            this.tlcServer.OptionsColumn.AllowMoveToCustomizationForm = false;
            this.tlcServer.OptionsColumn.AllowSize = false;
            this.tlcServer.OptionsColumn.AllowSort = false;
            this.tlcServer.OptionsColumn.FixedWidth = true;
            this.tlcServer.OptionsColumn.ReadOnly = true;
            this.tlcServer.Visible = true;
            this.tlcServer.VisibleIndex = 0;
            this.imageCollection2.ImageStream = (ImageCollectionStreamer)manager.GetObject("imageCollection2.ImageStream");
            this.imageCollection2.Images.SetKeyName(0x23, "Copy of network_local.png");
            this.imageCollection2.Images.SetKeyName(0x24, "network16.png");
            this.imageCollection2.Images.SetKeyName(0x25, "system16.png");
            this.imageCollection2.Images.SetKeyName(0x26, "My-Network16.png");
            this.imageCollection2.Images.SetKeyName(0x27, "computer16.png");
            this.imageCollection2.Images.SetKeyName(40, "My COMPUTER16.png");
            this.lblSelectdatabase.Location = new Point(240, 7);
            this.lblSelectdatabase.Name = "lblSelectdatabase";
            this.lblSelectdatabase.Size = new Size(0x40, 13);
            this.lblSelectdatabase.TabIndex = 0;
            this.lblSelectdatabase.Text = "Chọn Dữ Liệu";
            this.lblSelectdatabase.Click += new EventHandler(this.lblServer_Click);
            this.picRefreshdatabase.Cursor = Cursors.Hand;
            this.picRefreshdatabase.EditValue = manager.GetObject("picRefreshdatabase.EditValue");
            this.picRefreshdatabase.Location = new Point(480, 0x99);
            this.picRefreshdatabase.Name = "picRefreshdatabase";
            this.picRefreshdatabase.Properties.AllowFocused = false;
            this.picRefreshdatabase.Properties.Appearance.BackColor = Color.Transparent;
            this.picRefreshdatabase.Properties.Appearance.ForeColor = Color.Transparent;
            this.picRefreshdatabase.Properties.Appearance.Options.UseBackColor = true;
            this.picRefreshdatabase.Properties.Appearance.Options.UseForeColor = true;
            this.picRefreshdatabase.Properties.BorderStyle = BorderStyles.NoBorder;
            this.picRefreshdatabase.Properties.ReadOnly = true;
            this.picRefreshdatabase.Properties.ShowMenu = false;
            this.picRefreshdatabase.Properties.SizeMode = PictureSizeMode.Stretch;
            this.picRefreshdatabase.Size = new Size(0x19, 0x19);
            this.picRefreshdatabase.TabIndex = 0x24;
            this.picRefreshdatabase.MouseLeave += new EventHandler(this.PicRefreshdatabaseMouseLeave);
            this.picRefreshdatabase.MouseHover += new EventHandler(this.PicRefreshdatabaseMouseHover);
            this.picRefreshdatabase.Click += new EventHandler(this.btnDatabaseOther_Click);
            this.picRefreshserver.Cursor = Cursors.Hand;
            this.picRefreshserver.EditValue = manager.GetObject("picRefreshserver.EditValue");
            this.picRefreshserver.Location = new Point(480, 0x16);
            this.picRefreshserver.Name = "picRefreshserver";
            this.picRefreshserver.Properties.AllowFocused = false;
            this.picRefreshserver.Properties.Appearance.BackColor = Color.Transparent;
            this.picRefreshserver.Properties.Appearance.ForeColor = Color.Transparent;
            this.picRefreshserver.Properties.Appearance.Options.UseBackColor = true;
            this.picRefreshserver.Properties.Appearance.Options.UseForeColor = true;
            this.picRefreshserver.Properties.BorderStyle = BorderStyles.NoBorder;
            this.picRefreshserver.Properties.ReadOnly = true;
            this.picRefreshserver.Properties.ShowMenu = false;
            this.picRefreshserver.Properties.SizeMode = PictureSizeMode.Stretch;
            this.picRefreshserver.Size = new Size(0x19, 0x19);
            this.picRefreshserver.TabIndex = 0x24;
            this.picRefreshserver.ToolTip = "Nạp lại";
            this.picRefreshserver.ToolTipIconType = ToolTipIconType.Information;
            this.picRefreshserver.MouseLeave += new EventHandler(this.PicRefreshserverMouseLeave);
            this.picRefreshserver.MouseHover += new EventHandler(this.PicRefreshserverMouseHover);
            this.picRefreshserver.Click += new EventHandler(this.btnOrther_Click);
            this.spWait.Active = false;
            this.spWait.Color = Color.DarkGray;
            this.spWait.InnerCircleRadius = 10;
            this.spWait.Location = new Point(0xe9, 50);
            this.spWait.Name = "spWait";
            this.spWait.NumberSpoke = 0x19;
            this.spWait.OuterCircleRadius = 0x16;
            this.spWait.RotationSpeed = 100;
            this.spWait.Size = new Size(0x3e, 50);
            this.spWait.SpokeThickness = 4;
            this.spWait.TabIndex = 0x26;
            this.spWait.Text = "Vui l\x00f2ng đợi...";
            this.spWait.Visible = false;
            this.ckDataExample.Location = new Point(0x130, 0xb6);
            this.ckDataExample.Name = "ckDataExample";
            this.ckDataExample.Properties.AllowHtmlString = true;
            this.ckDataExample.Properties.Appearance.ForeColor = Color.Black;
            this.ckDataExample.Properties.Appearance.Options.UseForeColor = true;
            this.ckDataExample.Properties.Caption = "<color=red>Chạy Dữ Liệu Mẫu";
            this.ckDataExample.Size = new Size(0xa8, 0x12);
            this.ckDataExample.TabIndex = 0x27;
            this.ckDataExample.CheckedChanged += new EventHandler(this.ckDataExample_CheckedChanged);
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x204, 0xf2);
            base.Controls.Add(this.ckDataExample);
            base.Controls.Add(this.spWait);
            base.Controls.Add(this.picRefreshserver);
            base.Controls.Add(this.picRefreshdatabase);
            base.Controls.Add(this.trlServer);
            base.Controls.Add(this.cbxDatabase);
            base.Controls.Add(this.lblDatabase);
            base.Controls.Add(this.cbxServer);
            base.Controls.Add(this.btnHelp);
            base.Controls.Add(this.btnCancel);
            base.Controls.Add(this.btnLogin);
            base.Controls.Add(this.tePassword);
            base.Controls.Add(this.teLogin);
            base.Controls.Add(this.lblSqlpassword);
            base.Controls.Add(this.lblSqlusername);
            base.Controls.Add(this.rdgAuthentication);
            base.Controls.Add(this.lblSelectdatabase);
            base.Controls.Add(this.lblServer);
            this.DoubleBuffered = true;
            base.FormBorderStyle = FormBorderStyle.FixedDialog;
            base.Icon = (Icon)manager.GetObject("$this.Icon");
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "xfmDatabaseConfig";
            base.ShowInTaskbar = false;
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "Cấu H\x00ecnh Cơ Sở Dữ Liệu";
            base.TopMost = true;
            base.Load += new EventHandler(this.xfmLogin_Load);
            base.FormClosing += new FormClosingEventHandler(this.xfmLogin_FormClosing);
            this.rdgAuthentication.Properties.EndInit();
            this.tePassword.Properties.EndInit();
            this.teLogin.Properties.EndInit();
            this.cbxServer.Properties.EndInit();
            this.cbxDatabase.Properties.EndInit();
            this.trlServer.EndInit();
            this.imageCollection2.EndInit();
            this.picRefreshdatabase.Properties.EndInit();
            this.picRefreshserver.Properties.EndInit();
            this.ckDataExample.Properties.EndInit();
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        private void InitMultiLanguages()
        {
            this.Text = MultiLanguages.GetString("tbl_DatabaseConfig", "ConfigDatabase", this.Text);
            this.tlcServer.Caption = MultiLanguages.GetString("tbl_DatabaseConfig", "SelectServer", this.tlcServer.Caption);
            this.lblSelectdatabase.Text = MultiLanguages.GetString("tbl_DatabaseConfig", "SelectDatabase", this.lblSelectdatabase.Text);
            this.lblServer.Text = MultiLanguages.GetString("tbl_DatabaseConfig", "Server", this.lblServer.Text);
            this.lblSqlusername.Text = MultiLanguages.GetString("tbl_DatabaseConfig", "UserName", this.lblSqlusername.Text);
            this.lblSqlpassword.Text = MultiLanguages.GetString("tbl_DatabaseConfig", "Password", this.lblSqlpassword.Text);
            this.lblDatabase.Text = MultiLanguages.GetString("tbl_DatabaseConfig", "Database", this.lblDatabase.Text);
            this.ckDataExample.Text = MultiLanguages.GetString("tbl_DatabaseConfig", "RunExampleData", this.ckDataExample.Text);
            this.btnLogin.Text = MultiLanguages.GetString("tbl_DatabaseConfig", "Action", this.btnLogin.Text);
            this.btnCancel.Text = MultiLanguages.GetString("tbl_DatabaseConfig", "Close", this.btnCancel.Text);
            this.btnHelp.Text = MultiLanguages.GetString("tbl_DatabaseConfig", "Help", this.btnHelp.Text);
        }

        private void lblServer_Click(object sender, EventArgs e)
        {
        }

        private void loadserver()
        {
            this.RaiseStartedEventHander();
            DataTable data = SmoApplication.EnumAvailableSqlServers();
            this.RaiseCompleteEventHander(data);
        }

        private void PicRefreshdatabaseMouseHover(object sender, EventArgs e)
        {
            this.picRefreshdatabase.BorderStyle = BorderStyles.UltraFlat;
        }

        private void PicRefreshdatabaseMouseLeave(object sender, EventArgs e)
        {
            this.picRefreshdatabase.BorderStyle = BorderStyles.NoBorder;
        }

        private void PicRefreshserverMouseHover(object sender, EventArgs e)
        {
            this.picRefreshserver.BorderStyle = BorderStyles.UltraFlat;
        }

        private void PicRefreshserverMouseLeave(object sender, EventArgs e)
        {
            this.picRefreshserver.BorderStyle = BorderStyles.NoBorder;
        }

        private void radioGroup1_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool disable = this.rdgAuthentication.SelectedIndex == 0;
            this.DisableSQLServerAuthentication(disable);
        }

        private void RaiseCancelEventHander()
        {
            if (this.Cancel != null)
            {
                this.Cancel(this);
            }
        }

        private void RaiseCompleteEventHander(object data)
        {
            if (this.Complete != null)
            {
                this.Complete(this, data);
            }
        }

        private void RaiseDatabasedEventHander(object data)
        {
            if (this.Databased != null)
            {
                this.Databased(this, data);
            }
        }

        private void RaiseDatabaseErrorEventHander(string message)
        {
            if (this.DatabaseError != null)
            {
                this.DatabaseError(this, message);
            }
        }

        private void RaiseLoginedEventHander(string ConnectString)
        {
            if (this.Logined != null)
            {
                this.Logined(this, ConnectString);
            }
        }

        private void RaiseProcessEventHander(string Message)
        {
            if (this.Process != null)
            {
                this.Process(this, Message);
            }
        }

        private void RaiseStartedEventHander()
        {
            if (this.Started != null)
            {
                this.Started(this);
            }
        }

        private void ReadCofig()
        {
            DataTable table = new DataTable("SERVER");
            table.Columns.Add("server");
            table.Columns.Add("auth");
            table.Columns.Add("user");
            table.Columns.Add("pass");
            table.Columns.Add("database");
            table.Columns.Add("ConnectString");
            SqlHelper helper = new SqlHelper();
            FileInfo info = new FileInfo(this._serverParameters);
            if (info.Exists)
            {
                DataSet set = new DataSet();
                set.Tables.Add(table);
                set.ReadXml(this._serverParameters, XmlReadMode.Auto);
                try
                {
                    if (set.Tables[0].Rows.Count > 0)
                    {
                        helper.Server = set.Tables[0].Rows[0]["server"].ToString();
                        helper.Authentication = Convert.ToInt32(set.Tables[0].Rows[0]["auth"]) == 0;
                        helper.UserID = MyEncryption.Decrypt(set.Tables[0].Rows[0]["user"].ToString(), "963147", true);
                        helper.Password = MyEncryption.Decrypt(set.Tables[0].Rows[0]["pass"].ToString(), "963147", true);
                        helper.Database = set.Tables[0].Rows[0]["database"].ToString();
                        helper.ConnectionString = MyEncryption.Decrypt(set.Tables[0].Rows[0]["ConnectString"].ToString(), "963147", true);
                        this.cbxServer.Text = helper.Server;
                        this.teLogin.Text = helper.UserID;
                        this.tePassword.Text = helper.Password;
                        this.cbxDatabase.Text = helper.Database;
                        this.rdgAuthentication.SelectedIndex = helper.Authentication ? 0 : 1;
                        this.ckDataExample.Checked = helper.Database == "HRMExample";
                    }
                }
                catch (Exception exception)
                {
                    XtraMessageBox.Show(exception.Message, "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
            }
        }

        private void SaveConfig(string server, int auth, string user, string pass, string database, string connecstring)
        {
            try
            {
                DataSet set = new DataSet();
                DataTable table = new DataTable("SERVER");
                table.Columns.Add("server");
                table.Columns.Add("auth");
                table.Columns.Add("user");
                table.Columns.Add("pass");
                table.Columns.Add("database");
                table.Columns.Add("ConnectString");
                table.Rows.Clear();
                table.Rows.Add(new object[] { server, auth, MyEncryption.Encrypt(user, "963147", true), MyEncryption.Encrypt(pass, "963147", true), database, MyEncryption.Encrypt(connecstring, "963147", true) });
                set.Tables.Add(table);
                set.WriteXml(this._serverParameters);
            }
            catch (Exception exception)
            {
                XtraMessageBox.Show(exception.Message, "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }

        private void trlServer_FocusedNodeChanged(object sender, FocusedNodeChangedEventArgs e)
        {
            string str = e.Node.GetValue(this.tlcServer).ToString();
            switch (str)
            {
                case "M\x00e1y Đơn":
                    this.cbxDatabase.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
                    this.cbxServer.Text = Environment.MachineName + @"\PERFECT";
                    return;

                case "Mạng Nội Bộ":
                    this.cbxDatabase.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
                    if (!e.Node.HasChildren)
                    {
                        new Thread(new ThreadStart(this.loadserver)).Start();
                        this.trlServer.ExpandAll();
                    }
                    return;

                case "Mạng Internet":
                    this.cbxDatabase.Properties.TextEditStyle = TextEditStyles.Standard;
                    this.cbxServer.Text = "Perfect.com.vn";
                    return;
            }
            this.cbxServer.Text = str;
        }

        private void xfmLogin_Complete(object sender, object data)
        {
            if (this.trlServer.InvokeRequired)
            {
                CompleteEventHander method = new CompleteEventHander(this.xfmLogin_Complete);
                base.Invoke(method, new object[] { sender, data });
            }
            else
            {
                this.spWait.Visible = false;
                this.spWait.Active = false;
                DataTable table = (DataTable)data;
                if (table.Rows.Count > 0)
                {
                    this.cbxServer.Properties.Items.Clear();
                    this.trlServer.Nodes[1].Nodes.Clear();
                }
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    this.cbxServer.Properties.Items.Add(table.Rows[i]["Name"]);
                    this.trlServer.AppendNode(new object[] { table.Rows[i]["Name"] }, 1, 40, 40, -1);
                }
                this.cbxServer.SelectedIndex = 0;
                Cursor.Current = Cursors.Default;
                this.Disable(true);
            }
        }

        private void xfmLogin_Databased(object sender, object data)
        {
            if (this.trlServer.InvokeRequired)
            {
                DatabasedEventHander method = new DatabasedEventHander(this.xfmLogin_Databased);
                base.Invoke(method, new object[] { sender, data });
            }
            else
            {
                DataTable table = (DataTable)data;
                if (table != null)
                {
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        this.cbxDatabase.Properties.Items.Add(table.Rows[i][0]);
                    }
                }
                this.Disable(true);
                this.spWait.Visible = false;
                Cursor.Current = Cursors.Default;
                if (this.cbxDatabase.Properties.Items.Count > 0)
                {
                    this.cbxDatabase.SelectedIndex = -1;
                }
                else
                {
                    XtraMessageBox.Show("Kh\x00f4ng t\x00ecm thấy cơ sở dữ liệu trong m\x00e1y chủ n\x00e0y.", "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }

        private void xfmLogin_DatabaseError(object sender, string message)
        {
            if (this.trlServer.InvokeRequired)
            {
                DatabaseErrorEventHander method = new DatabaseErrorEventHander(this.xfmLogin_DatabaseError);
                base.Invoke(method, new object[] { sender, message });
            }
            else
            {
                XtraMessageBox.Show(this, message, "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                this.Disable(true);
                this.spWait.Visible = false;
                Cursor.Current = Cursors.Default;
            }
        }

        private void xfmLogin_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this._isCancel)
            {
                this.RaiseCancelEventHander();
            }
        }

        private void xfmLogin_Load(object sender, EventArgs e)
        {
        }

        private void xfmLogin_Started(object sender)
        {
            if (this.trlServer.InvokeRequired)
            {
                StartedEventHander method = new StartedEventHander(this.xfmLogin_Started);
                base.Invoke(method, new object[] { sender });
            }
            else
            {
                Cursor.Current = Cursors.WaitCursor;
                this.spWait.Visible = true;
                this.spWait.Active = true;
                this.Disable(false);
            }
        }

        public string ConnectionStringParameters
        {
            get
            {
                return string.Format("{0};{1};{2};{3};{4};{5}", new object[] { this.cbxServer.Text, this.rdgAuthentication.SelectedIndex, this.teLogin.Text, this.tePassword.Text, this.cbxDatabase.Text, this.GetServerConnectionString() });
            }
        }

        public bool IsExit
        {
            get
            {
                return this._exit;
            }
            set
            {
                this._exit = value;
            }
        }

        public string MyServer
        {
            get
            {
                return this.cbxServer.Text;
            }
        }

        public int Part
        {
            get
            {
                return this._part;
            }
            set
            {
                this._part = value;
            }
        }

        public string UserName
        {
            get
            {
                return this.cbxServer.Text;
            }
        }

        public delegate void CancelEventHander(object sender);

        private delegate void CompleteEventHander(object sender, object data);

        private delegate void DatabasedEventHander(object sender, object data);

        public delegate void DatabaseErrorEventHander(object sender, string message);

        public delegate void LoginedEventHander(object sender, string ConnectString);

        private delegate void ProcessEventHander(object sender, string Message);

        private delegate void StartedEventHander(object sender);
    }

}
